package com.huarui.companypackage.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.ewaf.framework.model.PagingInfo;
import org.ewaf.framework.model.PagingSummary;
import org.ewaf.framework.service.BaseService;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;

import com.huarui.companypackage.dao.ICompanyServiceDao;
import com.huarui.companypackage.model.CompanyService;
import com.huarui.companypackage.model.Service;
import com.huarui.part.util.PartOperateResultEnum;
import com.huarui.permission.service.PermissionService;

public class CompanyServerService extends BaseService {

	private final static Logger logger = Logger
			.getLogger(CompanyServerService.class);

	private PermissionService permissionService;

	private ICompanyServiceDao getDao() {
		return (ICompanyServiceDao) dao;
	}

	public void getCompanyServicesByCompanyId(
			PagingInfo<Integer, CompanyService> pagingInfo) {
		Integer companyId = pagingInfo.getCondition();
		PagingSummary summary = pagingInfo.getSummary();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("companyId", companyId);
		Integer count = getDao().getCompanyServiceCountByCondition(map);
		map.put("start", summary.getStart());
		map.put("limit", summary.getLimit());
		List<CompanyService> items = getDao().getCompanyServiceByCondition(map);
		pagingInfo.setItems(items);
		pagingInfo.setTotal(count);
	}

	public List<CompanyService> getCompanyServiceByCondition(
			Map<String, Object> map) {

		return getDao().getCompanyServiceByCondition(map);
	}

	/**
	 * 保存赠送给公司的服务
	 * 
	 * @param companyId
	 *            公司id
	 * @param presentServices
	 *            赠送的服务id列表
	 * @param services
	 *            所有可赠送的服务id列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> presentSave(final Integer companyId,
			final List<Integer> presentServices, final List<Service> services) {
		Map<String, Object> resultMap = (Map<String, Object>) transactionTemplate
				.execute(new TransactionCallback() {

					Map<String, Object> resultMap = new HashMap<String, Object>();

					@Override
					public Object doInTransaction(TransactionStatus status) {
						try {

							Map<String, Object> map = new HashMap<String, Object>();

							map.put("companyId", companyId);
							List<Integer> ids = new ArrayList<Integer>();
							for (Service service : services) {
								ids.add(service.getId());
								permissionService
										.deleteRolePermissionWithComIdAndServiceId(
												companyId, service.getId());
							}
							map.put("ids", ids);
							// 先删除所有已经赠送的服务
							getDao().dletePresentCompanyService(map);
							// 再增加这次赠送的服务
							for (Integer id : presentServices) {
								CompanyService cs = new CompanyService();
								cs.setCompanyId(companyId);
								for (Service service : services) {
									if (service.getId().equals(id)) {
										cs.setServiceCode(service.getCode());
										cs.setServiceId(id);
										cs.setServiceName(service.getName());
										break;
									}

								}

								if (!cs.getServiceId().equals(0)) {
									getDao().create(cs);
									permissionService
											.insertRolePermissionWithComIdAndServiceId(
													companyId,
													cs.getServiceId());
								}

							}
							resultMap.put(
									PartOperateResultEnum.SUCCESS.getMsg(),
									true);
							resultMap.put("msg",
									PartOperateResultEnum.OK_MSG_SUCCESS
											.getMsg());

						} catch (Exception e) {
							logger.error("companyService 赠送保存错误", e);
							status.setRollbackOnly();
							resultMap.put(
									PartOperateResultEnum.SUCCESS.getMsg(),
									false);
							resultMap.put("msg",
									PartOperateResultEnum.ERROR_MSG_DATABASE
											.getMsg());
						}

						return resultMap;
					}
				});
		return resultMap;
	}

	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

}
